package com.foreknow.dao.impl;

import com.foreknow.dao.GuesetbookDao;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.model.GuestBook;

import java.sql.SQLException;
import java.util.List;

public class GuestbookDaoImpl extends BaseDAO implements GuesetbookDao {
    @Override
    public boolean save(GuestBook gb) throws SQLException {
        String sql = "insert into guestbook(name,phone,email,title,content) values(?,?,?,?,?)";
        int i = jdbcTemplate.update(sql,gb.getName(),gb.getPhone(),gb.getEmail(),gb.getTitle(),gb.getContent());
        System.out.println(i);
        return i==1;
    }

    @Override
    public List<Object> getQueryAll() throws SQLException {
        EntityMapping mapping = mappingFactory.getMap("guestbookmapping");
        List<Object> list = jdbcTemplate.query("select * from guestbook",mapping);
        System.out.println(list.size());
        return  list;
    }

    @Override
    public GuestBook getQueryById(int id) throws SQLException {
        EntityMapping mapping = mappingFactory.getMap("guestbookmapping");
        List<Object> a = jdbcTemplate.query("select * from guestbook where id=?", mapping, id);
        if (a.size() > 0) {
            return ((GuestBook) a.get(0));//---===Guestbook gb = (Guestbook)     a.get(0); return gb;
        }
        return null;
    }

    @Override
    public boolean deleteById(int id) throws SQLException {
        int a = jdbcTemplate.update("delete from guestbook where id=?",id);
        System.out.println(a);
        return a==1;
    }
}
